package hdoj;

import java.io.BufferedInputStream;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

//带钥匙的迷宫
public class KeyMaze1885 {

	
	private static final char OBSTACLE = '#';
	private static final char START = '*';
	private static final char EXIT = 'X';
	
	private static final int BKEY = 0;
	private static final int YKEY = 1;
	private static final int RKEY = 2;
	private static final int GKEY = 3;
	
	public static final int[][] dir = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}};
	private static final int MAX = 102; 
	
	private int minStep = Integer.MAX_VALUE;
	
	private int rows, columns; 
	private char[][] maze = new char[MAX][MAX];
	
	private boolean[][][] visited = new boolean[MAX][MAX][20];
	
	private HashMap<Character, Integer> map = new HashMap<>();
	
	
	public static void main(String[] args) {
		KeyMaze1885 object = new KeyMaze1885();
		object.getRes();
	}
	
	public void getRes(){
		Scanner scanner = new Scanner(new BufferedInputStream(System.in));
		while(scanner.hasNext()){
			rows = scanner.nextInt();
			minStep = 0;
			columns = scanner.nextInt();
			if(rows == 0 && columns == 0){
				break;
			}
			int startX = -1, startY = 0;
			for(int i = 0; i < rows; i++){
				maze[i] = scanner.next().toCharArray();
				if(startX == -1){
					for(int j = 0; j < columns; j++){
						if(maze[i][j] == START){
							startX = i;
							startY = j;
							break;
						}
					}
				}
			}
			scanner.nextLine();
			scanner.nextLine();
			bfs(startX, startY);
			if(minStep == 0){
				System.out.println("The poor student is trapped!");
			}else{
				System.out.println("Escape possible in " + minStep + " steps.");
			}
			
		}
		scanner.close();
	}
	
	private class Node{
		int x, y;
		int step;
		int key;
		public Node(int x, int y){
			this.x = x;
			this.y = y;
		}
	}
	
	
	// Runtime 655MS
	public void bfs(int x, int y){
		Queue<Node> queue = new LinkedList<>();
		Node node = new Node(x, y);
		queue.add(node);
		map.put('B', 0);
		map.put('Y', 1);
		map.put('R', 2);
		map.put('G', 3);
		for (int i = 0; i < rows; i++) {
			for (int j = 0; j < columns; j++) {
				for (int k = 0; k < visited[0][0].length; k++) {
					visited[i][j][k] = false;
				}
			}
		}
		visited[x][y][0] = true;
		while(!queue.isEmpty()){
			node = queue.poll();
			// System.out.println("x: " + node.x + " y: " + node.y);
			if(maze[node.x][node.y] == EXIT){
				minStep = node.step;
				return;
			}
			for(int i = 0; i < dir.length; i++){
				int curX = node.x + dir[i][0];
				int curY = node.y + dir[i][1];
				if(curX < 0 || curX >= rows || curY < 0 || curY >= columns || maze[curX][curY] == OBSTACLE){
					continue;
				}
				if(visited[curX][curY][node.key]){
					continue;
				}
				Node next = new Node(curX, curY);
				next.key = node.key;
				if(maze[curX][curY] == 'B' || maze[curX][curY] == 'Y' || maze[curX][curY] == 'R'
						|| maze[curX][curY] == 'G'){
					if( (next.key & (1 << map.get(maze[curX][curY]))) == 0){
						continue;
					}
				}else if(maze[curX][curY] == 'b' || maze[curX][curY] == 'y' || maze[curX][curY] == 'r'
						|| maze[curX][curY] == 'g'){
					switch(maze[curX][curY]){
					case 'b':
						next.key |= 1 << BKEY;
						break;
					case 'y':
						next.key |= 1 << YKEY;
						break;
					case 'r':
						next.key |= 1 << RKEY;
						break;
					case 'g':
						next.key |= 1 << GKEY;
						break;
					}
				}
				next.step = node.step + 1;
				visited[curX][curY][next.key] = true;
				queue.add(next);
			}
		}
	}
}
